package com.gears_software.sxdc.dccx.ui.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.MyCarListAdapter;
import com.gears_software.sxdc.dccx.data.adapter.MyTripListAdapter;
import com.gears_software.sxdc.dccx.data.finder.IMemberFinder;
import com.gears_software.sxdc.dccx.data.finder.IVehicleFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.MemberModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.VehicleModel;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.orhanobut.logger.Logger;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by chezhenzhen on 2017/7/26.
 */

@EActivity(R.layout.activity_mycar)
public class MyCarActivity extends AppCompatActivity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    ListView list_car;
    @ViewById
    Button btn_add;
    @ViewById
    LinearLayout ly_remark;
    MyCarListAdapter adapter;

    @RestService
    IVehicleFinder vehicleFinder;
    @Bean
    GearsErrorHandler errorHandler;
    MaterialDialogProgress dialog;
    List<VehicleModel> vehicleModelList = new ArrayList<VehicleModel>();

    @AfterViews
    void created() {
        initIconFont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        vehicleFinder.setRestErrorHandler(errorHandler);
        dialog = new MaterialDialogProgress(this);
        dialog.show("正在加载...");
        getVehicleList();
    }

    void initIconFont() {
        actionbar_title.setText("车辆管理");
        back_icon.setVisibility(View.VISIBLE);
        back_icon.setText(R.string.back_icon);
        back_icon.setTypeface(app.iconfont);
        right_icon.setVisibility(View.GONE);
    }

    @Background
    public void getVehicleList() {
        ResultModel<List<VehicleModel>> res = vehicleFinder.getVehicleList();
        showResult(res);
    }

    @UiThread
    void showResult(ResultModel<List<VehicleModel>> res) {
        dialog.dismiss();
        if (res != null) {
            if (res.getErrno().equals("0")) {
                vehicleModelList = res.getData();
                if (vehicleModelList != null && vehicleModelList.size() > 0) {
                    adapter = new MyCarListAdapter(this, vehicleModelList);
                    list_car.setAdapter(adapter);
                    if (vehicleModelList.size() == 3) {
                        btn_add.setVisibility(View.GONE);
                    }else{
                        btn_add.setVisibility(View.VISIBLE);
                        btn_add.setText("添加第" + (vehicleModelList.size() + 1) + "辆车");
                    }
                } else if (vehicleModelList == null || vehicleModelList.size() == 0) {
                    adapter = new MyCarListAdapter(this, vehicleModelList);
                    list_car.setAdapter(adapter);
                    btn_add.setVisibility(View.VISIBLE);
                    btn_add.setText("添加车辆信息");
                }
                ly_remark.setVisibility(View.VISIBLE);
            }
        }
    }

    @Click
    void btn_add() {
        Intent intent = new Intent(this, AddCarInfoActivity_.class);
        intent.putExtra("title", "添加车辆");
        intent.putExtra("flag","add");
        startActivityForResult(intent, 1);
    }

    @OnActivityResult(value = 1)
    void onResult() {
        getVehicleList();
    }

    @Click
    void back_icon() {
        this.finish();
    }
}
